using System;

namespace ECommerce.Web.Models
{
    public interface IShopperWithBasket
    {
        Basket Basket { get; set; }
        Guid Id { get; set; }
        void AddProductToBasket(Product product, int quantity);
        void RemoveProductFromBasket(Product product);
        void ChangeProductQuantityInBasket(Product product, int quantity);
    }

    public class Shopper : EntityWithTypedId<Guid>, IShopperWithBasket
    {
        public virtual int Version { get; set; }

        private Basket basket;

        public virtual Basket Basket
        {
            get
            {
                if (this.basket == null)
                {
                     this.basket = GetEmptyBasket();
                }

                return this.basket;
            }
            set
            {
                this.basket = value;
            }
        }

        private Basket GetEmptyBasket()
        {
            var basket = new Basket();

            basket.Shopper = this;
            basket.DateIssued = SystemTime.Now();

            return basket;
        }

        public virtual void AddProductToBasket(Product product, int quantity)
        {
            Basket.AddProduct(product, quantity);
        }

        public virtual void RemoveProductFromBasket(Product product)
        {
            Basket.RemoveProduct(product);
        }

        public virtual void ChangeProductQuantityInBasket(Product product, int quantity)
        {
            Basket.ChangeProductQuantity(product, quantity);
        }
    }
}